// var fundebug = require('../libs/fundebug.1.7.4.min.js');
function get(url, data, cb) {
  console.log('请求路径：', url);
  console.log('请求参数：', data || {});
  // wx.showLoading({
  //   title: '加载中',
  // })
  wx.request({
    url: getApp().globalData.serverUrl + url,
    method: 'GET',
    enableHttp2: true,
    data: data || {},
    header: {
      "content-type": "application/json;charset=utf-8",
      'access-token': getApp().globalData.admin.token,
    },
    success: function (res) {
      // fundebug.notify("get wx.request success", JSON.stringify(res));
      console.log(url + '返回结果', res);
      wx.hideLoading();
      if (res.statusCode >= 400 && res.statusCode < 500) {

      } else if (res.statusCode == 500 || res.statusCode > 500) {
        wx.showToast({
          title: '服务器异常，请稍后重试',
          duration: 1500,
          icon: 'none'
        })
      } else if (res.statusCode == 200) {
        if (res.data.code != 0) {
          if (res.data.message.indexOf("token") != -1) {
            wx.clearStorageSync("admin_token")
            wx.redirectTo({
              url: '/pages/admin/login/index',
            })
          }
        }
        return typeof cb == "function" && cb(res.data)
      }
    },
    fail: function (err) {
      wx.hideLoading();
      wx.showToast({
        title: '网络异常，请检查网络设置(01)',
        duration: 1500,
        icon: 'none'
      })
      // fundebug.notify("get wx.request fail, version:" + getApp().globalData.miniVersion, JSON.stringify(err));
      return typeof cb == "function" && cb(false)
    }
  })
}
function post(url, data, cb) {
  console.log('请求路径：', url);
  console.log('请求参数：', data || {});
  // wx.showLoading({
  //   title: '加载中',
  // })
  var contentType = "application/json;charset=utf-8";
  var p = tansParams(data)
  if (p != '') {
    url = url + "?" + p;
  }
  wx.request({
    url: getApp().globalData.serverUrl + url,
    header: {
      "content-type": contentType,
      'access-token': getApp().globalData.admin.token,
    },
    data: data || {},
    method: 'POST',
    enableHttp2: true,
    timeout: 300000,
    success: function (res) {
      console.log(url + '返回结果', res);
      wx.hideLoading();
      if (res.statusCode >= 400 && res.statusCode < 500) {

      } else if (res.statusCode > 500 || res.statusCode == 500) {
        wx.showToast({
          title: '服务器异常，请稍后重试',
          duration: 1500,
          icon: 'none'
        })
      } else if (res.statusCode == 200) {
        if (res.data.code != 0) {
          if (res.data.message.indexOf("token") != -1) {
            wx.clearStorageSync("admin_token")
            wx.redirectTo({
              url: '/pages/admin/login/index',
            })
          }
        }
        return typeof cb == "function" && cb(res.data)
      }
    },
    fail: function (err) {
      wx.hideLoading();
      wx.showModal({
        content: '网络异常，请检查网络设置(02)',
        duration: 1500,
        icon: 'none'
      })
      console.log(JSON.stringify(err))
      // fundebug.notify("post wx.request fail, version:" + getApp().globalData.miniVersion, JSON.stringify(err));
      return typeof cb == "function" && cb(false)
    }
  })
}
function upload(url, filePath, cb) {
  console.log('请求路径：', url);
  console.log('上传路径', filePath);
  wx.uploadFile({
    url: getApp().globalData.serverUrl + url,
    header: {
      "content-type": "multipart/form-data",
      'access-token': getApp().globalData.admin.token,
    },
    filePath: filePath,
    name: 'file',
    timeout: 300000,
    success: function (res) {
      console.log(url + '返回结果', res);
      wx.hideLoading();
      if (res.statusCode >= 400 && res.statusCode < 500) {

      } else if (res.statusCode > 500 || res.statusCode == 500) {
        wx.showToast({
          title: '服务器异常，请稍后重试',
          duration: 1500,
          icon: 'none'
        })
      } else if (res.statusCode == 200) {
        if (res.data.code != 0) {
          var data = res.data;
          if ('object' != typeof object) {
            data = JSON.parse(data)
          }
          if (data.message.indexOf("token") != -1) {
            wx.clearStorageSync("admin_token")
            wx.redirectTo({
              url: '/pages/admin/login/index',
            })
          }
        }

        return typeof cb == "function" && cb(data)
      }
    },
    fail: function (err) {
      wx.hideLoading();
      wx.showModal({
        content: '上传失败,请重试',
        duration: 1500,
        icon: 'none',
        showCancel: false,
      })
      console.log(JSON.stringify(err))
      // fundebug.notify("post wx.request fail, version:" + getApp().globalData.miniVersion, JSON.stringify(err));
      return typeof cb == "function" && cb(false)
    }
  })
}
function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result
}
module.exports = {
  get: get,
  post: post,
  upload: upload,
  // postzdy: postzdy,
  // postAbsUrl, postAbsUrl,
  // requestGet: requestGet,
  // requestPost: requestPost,
  // postHeader: postHeader,
  // simplePost: simplePost,
}  